using UnityEngine;
using System.Collections;

public class NormalModeScript : GameModeBaseScript {

	public override void InitStateMechanic()
	{
		
		stateMechanics[(int)ModeState.State_None] = new StateMechanicScript( (int)ModeState.State_Num,
			new StateMechanicScript.MyEventHandler( this.SwitchToNone ),
			new StateMechanicScript.MyEventHandler( this.BreakNone ) );
		stateMechanics[(int)ModeState.State_None].SetStateChange( (int)ModeState.State_Idle, true );
		
		stateMechanics[(int)ModeState.State_Idle] = new StateMechanicScript( (int)ModeState.State_Num,
			new StateMechanicScript.MyEventHandler( this.SwitchToIdle ),
			new StateMechanicScript.MyEventHandler( this.BreakIdle ) );
		stateMechanics[(int)ModeState.State_Idle].SetStateChange( (int)ModeState.State_Fire, true );
		stateMechanics[(int)ModeState.State_Idle].SetStateChange( (int)ModeState.State_End, true );
		
		stateMechanics[(int)ModeState.State_Fire] = new StateMechanicScript( (int)ModeState.State_Num,
			new StateMechanicScript.MyEventHandler( this.SwitchToFire ),
			new StateMechanicScript.MyEventHandler( this.BreakFire ) );
		stateMechanics[(int)ModeState.State_Fire].SetStateChange( (int)ModeState.State_Idle, true );
		
		stateMechanics[(int)ModeState.State_End] = new StateMechanicScript( (int)ModeState.State_Num,
			new StateMechanicScript.MyEventHandler( this.SwitchToEnd ),
			new StateMechanicScript.MyEventHandler( this.BreakEnd ) );
		stateMechanics[(int)ModeState.State_End].SetStateChange( (int)ModeState.State_None, true );
	}
	
	
	
	
	
	
	public void SwitchToNone( object sender, System.EventArgs e )
	{
		curState = ModeState.State_None;
		//SwithState( ModeState.State_Idle, null );
	}
		
	public void BreakNone( object sender, System.EventArgs e )
	{
		InitScreen();
	}
			
	public void SwitchToIdle( object sender, System.EventArgs e )
	{
		curState = ModeState.State_Idle;
		
	}
		
	public void BreakIdle( object sender, System.EventArgs e )
	{
		
	}
	
	public void SwitchToFire( object sender, System.EventArgs e )
	{
		curState = ModeState.State_Fire;
		FireEvent fireEvent = e as FireEvent;
		//Ray ray = Camera.mainCamera.ScreenPointToRay (fireEvent.inPutPosition);
		//Vector3 p = Camera.mainCamera.ScreenToWorldPoint( fireEvent.inPutPosition );// (new Vector3(100, 100, camera.nearClipPlane));
		int result = screenMsg.TestFire( fireEvent.inPutRay );
		if ( result == 0 )
		{
			curTime -= 5.0f;
		}
		else if ( score > 0 )
		{
			score += result;
			gameMsg.SetScoreTex( score );
		}
		SwithState( ModeState.State_Idle, null );
	
	}
	
	public void BreakFire( object sender, System.EventArgs e )
	{
		
	}
	
	public void SwitchToEnd(  object sender, System.EventArgs e )
	{
		curState = ModeState.State_End;
		
	}
	
	public void BreakEnd(  object sender, System.EventArgs e )
	{
		screenMsg.DeleteDate();
		screenMsg.ReleaseScreen();
		
		//screenMsg.CreateSphere( percent );
		/*curState = ModeState.State_Idle;
		score = 0;
		curTime = gameTime;
		gameMsg.SetScoreTex( score );*/
		
	}
	
	
	public override void InitScreen()
	{
		screenMsg.SetScreenVh( gameMsg, Vertical, Horizontal );
		screenMsg.InitScreen();
		screenMsg.CreateSphere( percent );
		curState = ModeState.State_Idle;
		score = 0;
		curTime = gameTime;
		gameMsg.SetScoreTex( score );
	}
	
	public override void Update()
	{
		if ( curState == ModeState.State_End )
			return;
		if ( curTime > 0 )
		{
			curTime -= Time.deltaTime;
		}
		if ( screenMsg.curSphereNum == 0 || curTime < 0.0f )
		{
			SwithState( ModeState.State_End, null );
			gameMsg.SwithState( GameMsgScript.GameState.State_End, null );
		}
	}
	
	public override void SetDifficult(int difficult )
	{
		gameTime = 30.0f;
		
		Vertical = 10;
		Horizontal = 10;
		percent = 0.6f;
	}
	
	public float percent;
}
